import svcInvoke from "../svcinvoke";
export default {
	namespaced: true,
	state:{
		select: '',/* 点击选中的股票 */
		symbol:{},/* 全部行情外层 */
		symbos:{},/* 当前页面显示行情 */
		ticket:{},/* 动态行情 */
		optionlist:[],/* 全部行情 */
		activeI: 0,
		selectData: {},
		contactlist: [],
		Allcontactlist: []
	},
	mutations:{
		setAllContactList(state, item) {
			state.Allcontactlist = item
		},
		setContactList(state, item) {
			state.contactlist = item
		},
		select(state, item){
			state.select = item.M + item.S + item.C;
			state.selectData = item
		},
		setSymbol(state, payload) {
			let symbol = new Object(),symbols = Object.assign(new Array(),state.symbos);
			for(var key in payload) {
				if( payload[key]['title'] ){
					symbol[ payload[key]['title'] ] = payload[key];
				}
			}
      for(var key in payload) {
      	if( payload[key]['title'] ){
      		symbol[ payload[key]['title'] ] = payload[key];
      		if( !symbols[ payload[key]['title'] ] ){
              symbols.push(payload[key]);
      		}
          if( !state.select.code ){
          	let select   = Object.assign(new Object(),payload[key].symbols);
            for(var selectli in select){
              state.select = select[selectli];
              break;
            }
            break;
          }
      	}
      }
			state.symbol  = symbol;
			state.symbos  = symbols;
		},
    setSymbos(state, payload) {
    	let symbols = Object.assign(new Array(),payload);
      state.symbos  = symbols;
    },
		delSymbol(state, key) {
			if( state.symbol[key] ){
				delete state.symbol[key]
			}
		},
	setactiveI(state, activeI) {
		state.activeI = activeI
	},
    setTicket(state, ticket) {
    	// state.ticket = ticket;
		if (state.contactlist.length > 0) {
			state.ticket = ticket.map( item => {
				let ite = state.contactlist.find(data => data.contract == ( item.M + item.S + item.C ))
				if (ite) {
					item.unit = ite.unit
					item.unitprice = ite.unitprice
					item.isShow = true
					return item
				}
				let tec = state.Allcontactlist.find( data => data.contract == ( item.M + item.S + item.C ))
				if (tec) {
					item.unit = tec.unit
					item.unitprice = tec.unitprice
					item.isShow = false
					return item
				}
				
				return item
			})
		}
    },
		setOptionlist(state, symbol) {
			// console.log(symbol);
			//let optionlsit = new Array();
			let optionlsit = {};
			for(let i=0;i<symbol.length;i++){
				for(let j=0;j<symbol[i].symbols.length;j++){
				optionlsit[symbol[i].symbols[j]['code']] = symbol[i].symbols[j]
				//optionlsit.push(symbol[i].symbols[j])
				}
			}
			// console.log(optionlsit);
			state.optionlist = optionlsit;
		},
		delTicekt(state, code) {
			if( state.ticket[code] ){
				delete state.ticket[code]
			}
		}
	},
	actions:{
		// order_position ({ commit, state }, params) {
		// 	svcInvoke.get('addons/tf-futures/order/list?state=hold',params||{}).then(res=>{
		// 		if( res.code==200 ){
		// 			state.order_position = res.data
		// 		}
		// 	})
		// }
	}
}
